package org.dromara.apartment.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.apartment.domain.vo.ViewAppointmentVo;
import org.dromara.apartment.domain.bo.ViewAppointmentBo;
import org.dromara.apartment.service.IViewAppointmentService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 预约看房信息
 * 前端访问路由地址为:/apartment/viewAppointment
 *
 * @author CKJ
 * @date 2025-06-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/viewAppointment")
public class ViewAppointmentController extends BaseController {

    private final IViewAppointmentService viewAppointmentService;

    /**
     * 查询预约看房信息列表
     */
    @SaCheckPermission("apartment:viewAppointment:list")
    @GetMapping("/list")
    public TableDataInfo<ViewAppointmentVo> list(ViewAppointmentBo bo, PageQuery pageQuery) {
        return viewAppointmentService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出预约看房信息列表
     */
    @SaCheckPermission("apartment:viewAppointment:export")
    @Log(title = "预约看房信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ViewAppointmentBo bo, HttpServletResponse response) {
        List<ViewAppointmentVo> list = viewAppointmentService.queryList(bo);
        ExcelUtil.exportExcel(list, "预约看房信息", ViewAppointmentVo.class, response);
    }

    /**
     * 获取预约看房信息详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("apartment:viewAppointment:query")
    @GetMapping("/{id}")
    public R<ViewAppointmentVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable("id") Long id) {
        return R.ok(viewAppointmentService.queryById(id));
    }

    /**
     * 新增预约看房信息
     */
    @Log(title = "预约看房信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ViewAppointmentBo bo) {
        return toAjax(viewAppointmentService.insertByBo(bo));
    }

    /**
     * 修改预约看房信息
     */
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ViewAppointmentBo bo) {
        return toAjax(viewAppointmentService.updateByBo(bo));
    }

    /**
     * 删除预约看房信息
     *
     * @param ids 主键串
     */
    @SaCheckPermission("apartment:viewAppointment:remove")
    @Log(title = "预约看房信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable("ids") Long[] ids) {
        return toAjax(viewAppointmentService.deleteWithValidByIds(List.of(ids), true));
    }


    /**
     * 新增预约看房信息
     */
    @RepeatSubmit()
    @PostMapping("app-add")
    public R<Void> AppAdd(@Validated(AddGroup.class) @RequestBody ViewAppointmentBo bo) {
        return toAjax(viewAppointmentService.insertByBo(bo));
    }

    /**
     * 查询预约看房信息列表
     */
    @GetMapping("/app-page")
    public TableDataInfo<ViewAppointmentVo> appPage(ViewAppointmentBo bo, PageQuery pageQuery) {
        bo.setUserId(LoginHelper.getAppLoginUser().getId());
        return viewAppointmentService.queryPageList(bo, pageQuery);
    }
}
